#!/usr/bin/python3
#! Encoding UTF-8
import signal
import asyncio
 
class GracefulKiller:
    is_kill_now = False
 
    def __init__(self):
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)
        signal.signal(signal.SIGKILL, self.exit_gracefully)
        signal.signal(signal.SIGHUP, self.exit_gracefully)
 
    def exit_gracefully(self, signum, frame):
        self.is_kill_now = True
 
async def loop_task():
    killer = GracefulKiller()
    while 1:
        print("主进程运行中")  # 任务主体
        if killer.is_kill_now:
            break
        await asyncio.sleep(2)
 
 
loop = asyncio.new_event_loop()
try:
    loop.run_until_complete(loop_task())
finally:
    loop.run_until_complete(
        # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.shutdown_asyncgens
        loop.shutdown_asyncgens()) 
    loop.close()